"""
MySQL数据库配置
"""
import os
from dataclasses import dataclass
from typing import Optional
from dotenv import load_dotenv

# 加载环境变量
load_dotenv()

@dataclass
class DatabaseConfig:
    """数据库配置类"""
    host: str
    port: int
    user: str
    password: str
    database: str
    charset: str = 'utf8mb4'
    autocommit: bool = True
    
    @classmethod
    def from_env(cls) -> 'DatabaseConfig':
        """从环境变量创建配置"""
        return cls(
            host=os.getenv('MYSQL_HOST', 'localhost'),
            port=int(os.getenv('MYSQL_PORT', 3306)),
            user=os.getenv('MYSQL_USER', 'root'),
            password=os.getenv('MYSQL_PASSWORD', 'quantitative_password'),
            database=os.getenv('MYSQL_DATABASE', 'quantitative_advanced'),
            charset=os.getenv('MYSQL_CHARSET', 'utf8mb4'),
            autocommit=bool(os.getenv('MYSQL_AUTOCOMMIT', True))
        )
    
    def to_dict(self) -> dict:
        """转换为字典格式"""
        return {
            'host': self.host,
            'port': self.port,
            'user': self.user,
            'password': self.password,
            'database': self.database,
            'charset': self.charset,
            'autocommit': self.autocommit
        }

# 默认数据库配置
db_config = DatabaseConfig.from_env()

# 连接池配置
POOL_CONFIG = {
    'pool_name': 'quantitative_pool',
    'pool_size': 10,
    'pool_reset_session': True,
    'buffered': True,
    'raise_on_warnings': True
}
